package com.example.mykotilin.paging2.db

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.mykotilin.paging2.vo.RedditPost
import com.example.mykotilin.paging2.vo.SubredditRemoteKey

@Database(
    entities = arrayOf(RedditPost::class, SubredditRemoteKey::class),
    version = 1,
    exportSchema = false
)
abstract class RedditDb : RoomDatabase() {
    companion object {
        fun create(context: Context, useInMemory: Boolean): RedditDb {
            val databaseBuilder = if (useInMemory) {//使用内存
                Room.inMemoryDatabaseBuilder(context, RedditDb::class.java)
            } else {//使用数据库，就会创建一个数据库
                Room.databaseBuilder(context, RedditDb::class.java, "reddit.db")
            }
            return databaseBuilder.fallbackToDestructiveMigration().build()
        }
    }

    abstract fun posts(): RedditPostDao
    //外键
    abstract fun remoteKeys(): SubredditRemoteKeyDao
}